//
//  SettingsViewController.m
//  XDCommonApp
//
//  Created by wanglong8889@126.com on 14-6-12.
//  Copyright (c) 2014年 XD-XY. All rights reserved.
//

#import "SettingsViewController.h"
#import "XRSetCell.h"
#import "AboutViewController.h"
#import "FeedBackViewController.h"
#import "LiuChengViewController.h"
#import "MoreQuestionViewController.h"
#import "ReplyViewController.h"
@interface SettingsViewController ()
{
    BOOL isBangQQ;
    BOOL isBangWeibo;
    UILabel *label;
    NSString *downUrl;
    
    NSString *cString;
   
}
@end

@implementation SettingsViewController

- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
        // Custom initialization
    }
    return self;
}

- (void)viewDidLoad
{
    [super viewDidLoad];
    
    self.titleLabel.text = @"设置";
   
    NSDictionary *dic = [[NSDictionary alloc] init];
    [self getGradeUrlParms:dic];
    [self initSettingsViews];
    // Do any additional setup after loading the view.
}


-(void)initSettingsViews{
    dataArray =[[NSMutableArray alloc]initWithObjects:@"意见反馈",@"缓存清理",@"给我们打个分吧",@"检测新版本",@"关于", nil];
    
    
    mTableView =[[UITableView alloc]initWithFrame:CGRectMake(0, 0,UI_SCREEN_WIDTH , self.contentView.frame.size.height-2) style:UITableViewStylePlain];
    mTableView.delegate =self;
    mTableView.dataSource =self;
    mTableView.showsVerticalScrollIndicator = NO;
    mTableView.separatorStyle = UITableViewCellSeparatorStyleNone;
    mTableView.backgroundColor =[UIColor clearColor];
    [self.contentView addSubview:mTableView];
}



#pragma mark tableView delegate and datasouce
-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    static NSString *ID = @"ID";
    XRSetCell *cell =[tableView dequeueReusableCellWithIdentifier:ID];
    if(!cell){
        cell = [[XRSetCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:ID];
        cell.selectionStyle = UITableViewCellSelectionStyleNone;
        cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator;
        cell.mlabel.hidden = YES;
        if(!indexPath.section){
            if(!indexPath.row){
                cell.contentLabel.text =@"支付流程";
            }else if (indexPath.row == 1){
                cell.contentLabel.text =@"常见问题";
            }else if (indexPath.row == 2){
                cell.contentLabel.text =@"反馈我们";
            }
        }else{
            if(!indexPath.row){
                cell.contentLabel.text =@"给我们打个分吧";
            }else if (indexPath.row == 1){
                cell.contentLabel.text =@"版本升级";
            }else if (indexPath.row == 2){
                cell.contentLabel.text =@"关于猫贷信用支付";
            }
        }

        if (indexPath.row == 0) {
            cell.upImageView.hidden = NO;
            cell.downImageView.hidden = NO;
        }else{
            cell.upImageView.hidden = YES;
            cell.downImageView.hidden = NO;
        }
    }
    return cell;
}
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{
    return 2;
}
-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    return 3;
}

- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section {
    UIView *view = [[UIView alloc] init];
    view.backgroundColor = [UIColor clearColor];
    return view;
}

- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section{
    return 25.0;
}

-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    if (!indexPath.section && indexPath.row == 0) {
        LiuChengViewController * liucheng = [[LiuChengViewController alloc] init];
        [self.navigationController pushViewController:liucheng animated:YES];
    }else if (!indexPath.section &&indexPath.row == 1) {
        MoreQuestionViewController * morequestion = [[MoreQuestionViewController alloc] init];
        [self.navigationController pushViewController:morequestion animated:YES];
    }else if (!indexPath.section &&indexPath.row == 2) {
        ReplyViewController * reply = [[ReplyViewController alloc] init];
        [self.navigationController pushViewController:reply animated:YES];
    }
    if (indexPath.section) {
        switch (indexPath.row) {
            case 0:
            {
                [[UIApplication sharedApplication] openURL:[NSURL URLWithString:appStoreUrl]];
            }
                break;
                
            case 1:
            {
                [self updateVerson];
            }
                break;
                
            case 2:
            {
                AboutViewController * about = [[AboutViewController alloc] init];
                [self.navigationController pushViewController:about animated:YES];
            }
                break;
                
            default:
                break;
        }
    }
    
    if (indexPath.section == 3) {
        AboutViewController *aboutVC = [[AboutViewController alloc] init];
        [self.navigationController pushViewController:aboutVC animated:YES];
    }
    
}


//清理缓存

-(void)clearMemory{
    dispatch_async(
                   dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0)
                   , ^{
                       NSString *cachPath = [NSSearchPathForDirectoriesInDomains(NSCachesDirectory, NSUserDomainMask, YES) objectAtIndex:0];
                       
                       NSArray *files = [[NSFileManager defaultManager] subpathsAtPath:cachPath];
                       NSLog(@"files :%d",[files count]);
                       for (NSString *p in files) {
                           NSError *error;
                           NSString *path = [cachPath stringByAppendingPathComponent:p];
                           if ([[NSFileManager defaultManager] fileExistsAtPath:path]) {
                               [[NSFileManager defaultManager] removeItemAtPath:path error:&error];
                           }
                       }
                       dispatch_async(dispatch_get_main_queue(), ^{
                           [XDTools showProgress:self.view showText:@"正在清理"];
                           dispatch_async(dispatch_get_global_queue(0, 0), ^{
                               
                               sleep(1.0);
                               dispatch_async(dispatch_get_main_queue(), ^{
                                   [XDTools hideProgress:self.view];
                                   
                                   label.text=@"0kb";
                               });
                           });
                       });});
}


//获取打分的url


- (void)getGradeUrlParms:(NSDictionary *)parms{
    
    __weak  ASIHTTPRequest * request =[ASIHTTPRequest requestWithURL:[NSURL URLWithString:API_GRADE]];
    
    [request setCompletionBlock:^{
        [XDTools hideProgress:self.view];
        
        NSDictionary *tempDic = [XDTools  JSonFromString:[request responseString]];
        if([[tempDic objectForKey:@"result"]intValue] == 1){
            
            appStoreUrl = tempDic[@"data"][@"url"];
            
        }
    }];
    
    [request setFailedBlock:^{
        
        
        NSError *error = [request error];
        DDLOG(@"error=%@",error);
    }];
    
    
    [request startAsynchronous];
    
}


//版本更新
-(void)updateVerson{
    
    if(![XDTools NetworkReachable]){
        return;
    }
    
    __weak  ASIHTTPRequest * request =[ASIHTTPRequest requestWithURL:[NSURL URLWithString:API_UPDATEVERSION]];
    [request setCompletionBlock:^{
        NSDictionary * tmpDict= [XDTools JSonFromString:[request responseString]];
        
        if ([[tmpDict objectForKey:@"result"] intValue] == 1) {
            NSDictionary * dict = tmpDict[@"data"][@"ios"];
            
            NSString *oldVerson = [[NSString alloc] initWithString:[[[NSBundle mainBundle] infoDictionary] objectForKey:@"CFBundleShortVersionString"]];
            
            NSString *newVerson = [dict valueForKey:@"version"];
            
            downUrl = [dict objectForKey:@"url"];
            cString = [dict objectForKey:@"content"];
            
            if ([newVerson isEqualToString:oldVerson]) {
                [XDTools showTips:@"已是最新版本" toView:self.view];
            }else{
                UIAlertView *al = [[UIAlertView alloc] initWithTitle:@"提示" message:cString delegate:self cancelButtonTitle:@"确定" otherButtonTitles:@"取消", nil];
                al.tag = 1000;
                [al show];
            }
        }else{
            [XDTools showTips:[tmpDict objectForKey:@"msg"] toView:self.view];
        }
        
        
        
    }];
    
    [request setFailedBlock:^{
        
    }];
    
    [request startAsynchronous];
}


#pragma mark- UIAlertViewDelegate
- (void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex
{
    if (alertView.tag == 1000){
        if (buttonIndex == 0) {
            NSURL *url = [NSURL URLWithString:downUrl];
            [[UIApplication sharedApplication] openURL:url];
            
        }
    }
    
}
- (void)didReceiveMemoryWarning
{
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender
{
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
